package v3.chapter2.synTwoLock; 

public class Run {

	public static void main(String[] args) {
		Service service=new Service();
		ThreadA a=new ThreadA(service);
		a.setName("a");
		a.start();
		
		ThreadB b=new ThreadB(service);
		b.setName("b");
		b.start();
		
		ThreadC c=new ThreadC(service);
		c.setName("c");
		c.start();
	}
}

class ThreadA extends Thread{
	private Service service;
	public ThreadA(Service service) {
		super();
		this.service=service;
	}
	
	public void run() {
		service.printA();
	}
}

class ThreadB extends Thread{
	private Service service;
	public ThreadB(Service service) {
		super();
		this.service=service;
	}
	
	public void run() {
		service.printB();
	}
}

class ThreadC extends Thread{
	private Service service;
	public ThreadC(Service service) {
		super();
		this.service=service;
	}
	
	public void run() {
		service.printC();
	}
}

class Service {
	synchronized public static void printA() {
		try {
			System.out.println( "线程名称：" + Thread.currentThread().getName()+" 在 " + System.currentTimeMillis()+"进入 printA" );
			Thread.sleep(2000);
			System.out.println( "线程名称：" + Thread.currentThread().getName()+" 在 " + System.currentTimeMillis()+"离开 printA" );
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	synchronized public static void printB() {
		try {
			System.out.println( "线程名称：" + Thread.currentThread().getName()+" 在 " + System.currentTimeMillis()+"进入 printB" );
			Thread.sleep(2000);
			System.out.println( "线程名称：" + Thread.currentThread().getName()+" 在 " + System.currentTimeMillis()+"离开 printB" );
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	synchronized public void printC() {
		try {
			System.out.println( "线程名称：" + Thread.currentThread().getName()+" 在 " + System.currentTimeMillis()+"进入 printC" );
			Thread.sleep(2000);
			System.out.println( "线程名称：" + Thread.currentThread().getName()+" 在 " + System.currentTimeMillis()+"离开 printC" );
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}